package com.gas.project.gas.student.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.gas.common.utils.DateUtils;
import com.gas.common.utils.StringUtils;
import com.gas.common.utils.poi.ExcelUtil;
import com.gas.framework.aspectj.lang.annotation.Log;
import com.gas.framework.aspectj.lang.enums.BusinessType;
import com.gas.framework.web.controller.BaseController;
import com.gas.framework.web.domain.AjaxResult;
import com.gas.framework.web.page.TableDataInfo;
import com.gas.project.gas.grade.domain.CourseGrade;
import com.gas.project.gas.grade.domain.CourseValidGrade;
import com.gas.project.gas.grade.service.ICourseGradeService;
import com.gas.project.gas.resit.domain.CourseResit;
import com.gas.project.gas.resit.service.ICourseResitService;
import com.gas.project.gas.student.domain.GraduateStudent;
import com.gas.project.gas.student.domain.StudentAuditDetail;
import com.gas.project.system.user.domain.User;
import com.gas.project.system.user.service.IUserService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gas.project.gas.student.domain.Student;
import com.gas.project.gas.student.service.IStudentService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生信息管理Controller
 *
 * @author xyl
 * @date 2022-04-01
 */
@Controller
@RequestMapping("/gas/student")
public class StudentController extends BaseController {
    private String prefix = "gas/student";

    @Autowired
    private IStudentService studentService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ICourseGradeService courseGradeService;
    @Autowired
    private ICourseResitService courseResitService;

    @RequiresPermissions("gas:student:view")
    @GetMapping()
    public String student() {
        return prefix + "/student";
    }

    @RequiresPermissions("gas:student:graduate:view")
    @GetMapping("/graduate")
    public String graduate() {
        return prefix + "/graduate/graduate";
    }

    @RequiresPermissions("gas:student:excellent:view")
    @GetMapping("/excellent")
    public String excellent() {
        return prefix + "/excellent/excellent";
    }

    @RequiresPermissions("gas:student:evaluation")
    @GetMapping("/evaluation/{studentNumber}")
    public String evaluation(@PathVariable("studentNumber") String studentNumber, ModelMap mmap) {
        Student student = studentService.selectStudentById(studentNumber);
        mmap.put("student",student);
        return prefix + "/evaluation";
    }

    @RequiresPermissions("gas:student:graduate:audit")
    @GetMapping("/graduate/audit/{studentNumber}")
    public String audit(@PathVariable("studentNumber") String studentNumber, ModelMap mmap) {
        Student student = studentService.selectStudentById(studentNumber);
        mmap.put("student",student);
        return prefix + "/graduate/audit";
    }

    @RequiresPermissions("gas:student:excellent:audit")
    @GetMapping("/excellent/audit/{studentNumber}")
    public String excellentAudit(@PathVariable("studentNumber") String studentNumber, ModelMap mmap) {
        Student student = studentService.selectStudentById(studentNumber);
        mmap.put("student",student);
        return prefix + "/excellent/audit";
    }

    @RequiresPermissions("gas:student:excellent")
    @PostMapping("/excellent")
    @ResponseBody
    public AjaxResult excellent(Student student) {
        student.setIsExcellent("待审核");
        return toAjax(studentService.updateStudent(student));
    }

    /**
     * 查询学生信息管理列表
     */
    @RequiresPermissions("gas:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Student student) {
        User sysUser = getSysUser();
        if(sysUser.isTeacher()){
            student.setTeacherNumber(sysUser.getLoginName());
        }
        startPage();
        List<Student> list = studentService.selectStudentList(student);
        return getDataTable(list);
    }

    /**
     * 查询学生信息管理列表
     */
    @RequiresPermissions("gas:student:excellent:list")
    @PostMapping("/excellent/list")
    @ResponseBody
    public TableDataInfo excellentList(Student student) {
        startPage();
        List<Student> list = studentService.selectStudentList(student);
        return getDataTable(list);
    }

    /**
     * 查询学生信息管理列表
     */
    @RequiresPermissions("gas:student:graduate:list")
    @PostMapping("/graduate/list")
    @ResponseBody
    public TableDataInfo graduateList(GraduateStudent graduateStudent) {
        startPage();
        List<GraduateStudent> list = studentService.selectGraduateStudentList(graduateStudent);
        return getDataTable(list);
    }

    /**
     * 查询学生信息管理列表
     */
    @RequiresPermissions("gas:student:detail")
    @GetMapping("/detail/{studentNumber}")
    public String detail(@PathVariable("studentNumber") String studentNumber,ModelMap mmap) {
        //获取有效成绩数据
        StudentAuditDetail sad = studentService.selectStudentAuditDetail(studentNumber);
        mmap.put("sad",sad);
        mmap.put("user",getSysUser());
        return prefix + "/detail";
    }

    /**
     * 导出学生信息管理列表
     */
    @RequiresPermissions("gas:student:excellent:export")
    @Log(title = "学生信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/excellent/export")
    @ResponseBody
    public AjaxResult exportGraduate(Student student) {
        List<Student> list = studentService.selectStudentList(student);
        ExcelUtil<Student> util = new ExcelUtil<>(Student.class);
        return util.exportExcel(list, "student");
    }

    /**
     * 导出学生信息管理列表
     */
    @RequiresPermissions("gas:student:graduate:export")
    @Log(title = "学生信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/graduate/export")
    @ResponseBody
    public AjaxResult exportGraduate(GraduateStudent graduateStudent) {
        List<GraduateStudent> list = studentService.selectGraduateStudentList(graduateStudent);
        ExcelUtil<GraduateStudent> util = new ExcelUtil<GraduateStudent>(GraduateStudent.class);
        return util.exportExcel(list, "student");
    }

    /**
     * 导出学生信息管理列表
     */
    @RequiresPermissions("gas:student:export")
    @Log(title = "学生信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Student student) {
        List<Student> list = studentService.selectStudentList(student);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        return util.exportExcel(list, "student");
    }

    /**
     * 新增学生信息管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存学生信息管理
     */
    @RequiresPermissions("gas:student:add")
    @Log(title = "学生信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Student student) {
        student.setCreateBy(getLoginName());
        return toAjax(studentService.insertStudent(student));
    }

    /**
     * 修改学生信息管理
     */
    @GetMapping("/edit/{studentNumber}")
    public String edit(@PathVariable("studentNumber") String studentNumber, ModelMap mmap) {
        Student student = studentService.selectStudentById(studentNumber);
        mmap.put("student", student);
        return prefix + "/edit";
    }

    /**
     * 修改保存学生信息管理
     */
    @RequiresPermissions("gas:student:edit")
    @Log(title = "学生信息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Student student) {
        student.setUpdateBy(getLoginName());
        return toAjax(studentService.updateStudent(student));
    }

    /**
     * 删除学生信息管理
     */
    @RequiresPermissions("gas:student:remove")
    @Log(title = "学生信息管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(studentService.deleteStudentByIds(ids));
    }


    @RequiresPermissions("gas:student:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<Student> util = new ExcelUtil<>(Student.class);
        return util.importTemplateExcel("学生数据");
    }


    @Log(title = "学生管理" , businessType = BusinessType.IMPORT)
    @RequiresPermissions("gas:student:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        List<Student> userList = util.importExcel(file.getInputStream());
        //数据校验
        for (Student student : userList) {
            student.setCreateBy(getLoginName());
            student.setCreateTime(DateUtils.getNowDate());
            String check = student.check(student);
            if(StringUtils.isNotEmpty(check)){
                return error(check);
            }
        }
        List<Student> students = studentService.selectStudentByStudentNumbers(userList.stream().map(Student::getStudentNumber).collect(Collectors.toList()));
        if(students.size() > 0){
            return error(students.stream().map(Student::getStudentNumber).collect(Collectors.joining(",")) + "；以上学号已存在！");
        }
        String message = studentService.importStudent(userList);
        return AjaxResult.success(message);
    }

}
